<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {
    public $page_name = "article";
    /*public $config = array(
    array(
        'field' => 'title',
        'label' => '标题',
        'rules' => 'required'
    ),
    array(
        'field' => 'catalog_id',
        'label' => '分类',
        'rules' => 'required',
    ),
    array(
        'field' => 'desc',
        'label' => '简介',
        'rules' => 'required'
    ),
    array(
        'field' => 'content',
        'label' => '内容',
        'rules' => 'required'
    ),
    array(
        'field' => 'img',
        'label' => '图片',
        'rules' => 'required'
    )
);*/
   public function __construct(){
    parent::__construct();
    if(!$this->session->userdata('is_login')){
            redirect('/admin/auth/login');
        }
    }
	public function index($page=1){
        $this->load->model('Article_model');
        $total = $this->Article_model->count_all();
        $page_size = 3;
        $data=array(
            'articles'=>$this->Article_model->get_by_page($page,$page_size),
        );
        $config['use_page_numbers'] = TRUE;    
        $this->load->library('pagination');
        $config['base_url'] = site_url('/admin/article/index/');
        $config['total_rows'] = $total;
        $config['per_page'] = $page_size; 

        $this->pagination->initialize($config);
        $this->load->view('admin/Article_index',$data);
	}
    public function add(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', '标题', 'required');
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('desc', '简介', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');
        $this->load->model('Catalog_model');
        $article = new stdClass();
        $article->title="";
        $article->catalog_id=0;
        $article->desc="";
        $article->content="";
        $article->img="";
        $data = array(
                'catalogs'=>$this->Catalog_model->getall(),
                'title'=>"添加",
                'article'=>$article
                );
        if ($this->form_validation->run() == FALSE){
                $this->load->view('/admin/article_form',$data);
            }else{ 
                $data = $this->input->post();
                $this->load->model('Article_model');
                $this->Article_model->add_one($data);
                redirect('/admin/article/index',$data);
        }
    }
    public function eidt($id){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', '标题', 'required');
        $this->form_validation->set_rules('catalog_id', '分类', 'required');
        $this->form_validation->set_rules('desc', '简介', 'required');
        $this->form_validation->set_rules('content', '内容', 'required');
        $this->form_validation->set_rules('img', '图片', 'required');
        $this->load->model('Catalog_model');
        $this->load->model('Article_model');
        if ($this->form_validation->run() == FALSE){
            $data = array(
                'catalogs'=>$this->Catalog_model->getall(),
                'title'=>"编辑",
                'article'=>$this->Article_model->get_one($id)
            );
                $this->load->view('/admin/article_form',$data);
            }else{ 
                $data = $this->input->post();
                $this->Article_model->edit_one($id,$data);
                redirect('/admin/article/index');
        }
    }
    public function del($id){
        $this->load->model('Article_model');
        $data = array(
            'article'=>$this->Article_model->del($id)
            );
        redirect('/admin/article/index',$data );
    }
    public function recommend($id,$recommend){
        $this->load->model('Article_model');
        $this->Article_model->recommend($id,$recommend);
        redirect('/admin/article/index');
    }

}
